package com.stock.stockana.requests;

import android.util.Log;

import com.alibaba.fastjson.JSON;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.stock.publiclib.utils.StringUtil;
import com.stock.stockana.beans.SUser;
import com.stock.stockana.constants.NetWorkConstants;
import com.stock.stockana.events.network.GetOneStockEvent;
import com.stock.stockana.events.network.GetUserInfoEvent;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;


/**
 * ---------------------------------------------------
 * Description: 根据股票号码获取指定stock的请求类
 * Author: ljgsonx
 * Belong to: com.wds.stock.stockana.requests
 * Time: 2016/1/15 17:42
 * ---------------------------------------------------
 */
public class GetUserInfo extends BaseStringRequest<GetUserInfo> {

    private final static String TAG = "GetUserInfo--->";

    private GetUserInfo(){
        super(Method.POST, NetWorkConstants.GET_USERINFO_URL, listener, errorListener);
        Log.d(TAG, NetWorkConstants.GET_USERINFO_URL);
    }

    static Response.Listener<String> listener = new Response.Listener<String>(){

        @Override
        public void onResponse(String response) {
            if(StringUtil.isBlank(response)){
                EventBus.getDefault().post(new GetUserInfoEvent(NetWorkConstants.RESPONSE_CODE_ERROR,null));
            }else if("{".charAt(0) == response.charAt(0)) {
                SUser sUser = JSON.parseObject(response, SUser.class);
                GetUserInfoEvent event = new GetUserInfoEvent(NetWorkConstants.RESPONSE_CODE_OK, sUser);
                EventBus.getDefault().post(event);
            }else{
                EventBus.getDefault().post(new GetUserInfoEvent(NetWorkConstants.RESPONSE_CODE_ERROR,null));
            }
        }
    };

    static Response.ErrorListener errorListener = new Response.ErrorListener(){

        @Override
        public void onErrorResponse(VolleyError error) {
            EventBus.getDefault().post(new GetOneStockEvent(NetWorkConstants.RESPONSE_CODE_ERROR, null));
        }
    };

    @Override
    public Request<?> setTag(Object tag) {
        return super.setTag(TAG);
    }

    public static String getTagStr(){
        return TAG;
    }

    public static GetUserInfo build() {
        return new GetUserInfo();
    }

}
